import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
var car = JSON.parse(localStorage.getItem('car') || '[]')
var store = new Vuex.Store({
  state: {
    car: car
  },
  mutations: {
    addToCar(state, goodsinfoinstore) {

      //goodsinfoinstore 这个参数是从Goodsinfo的组件中 通过this.$store.commit('addToCar',goodsinfoInStore);传递过来的一个对象

      //假设在购物车中没有找到对应的商品
      var flag = false;
      state.car.some(item => {
        if (item.id == goodsinfoinstore.id) {
          item.count += parseInt(goodsinfoinstore.count);
          flag = true;
          return true;
        }
      })
      if (!flag) {
        state.car.push(goodsinfoinstore);
      }
      //更新car之后，把car存储到本地localstorage 中
      localStorage.setItem('car', JSON.stringify(state.car))
    },
    updateGoodsInfo(state, goodsinfoinstore) {
      state.car.some(item => {
        if (item.id == goodsinfoinstore.id) {
          item.count = parseInt(goodsinfoinstore.count);
          return true
        }
      })
      localStorage.setItem('car', JSON.stringify(state.car));
    },
    removeFromCar(state, id) {
      state.car.some((item, i) => {
        if (item.id == id) {
          state.car.splice(i, 1)
          // return ture 是跳出 some 循环
          return true
        }
      })
      localStorage.setItem('car', JSON.stringify(state.car));
    },
    updateGoodsSelected(state, info) {
      state.car.some(item => {
        if (item.id == info.id) {
          item.selected = info.selected;
        }
      })
      localStorage.setItem('car', JSON.stringify(state.car));
    },
  },
  getters: {
    getAllCount(state) {
      var c = 0;
      state.car.forEach(item => {
        c += item.count
      })
      return c
    },
    getGoodsCount(state) {
      var o = {};
      state.car.forEach(item => {
        o[item.id] = item.count;

      })
      return o
    },
    getGoodsSelected(state) {
      var o = {};
      state.car.forEach(item => {
        o[item.id] = item.selected;
      })
      return o
    },
    getGoodsCountAndAmount(state) {
      var o = {
        count: 0,
        amount: 0
      }
      state.car.some(item => {
        // selected 是同过 goodsinfo 组件中this.$store.commit('addToCar',goodsinfoInStore); 第二个参数传过去的 默认为true
        if (item.selected) {
          o.count += item.count
          o.amount += item.price * item.count
        }
      })
      return o
    }
  },
})
Vue.config.devtools = true
import VueResource from 'vue-resource'
Vue.use(VueResource)
import VueRouter from 'vue-router'
Vue.http.options.emulateJSON = true
import moment from 'moment'
Vue.use(VueRouter)
Vue.filter('dateFomat', function (datastr, patern = "YYYY-MM-DD HH:mm:ss") {
  return moment(datastr).format(patern)
})
import router from './router.js'
import app from './app.vue'
import './lib/mui/css/mui.min.css'
import './lib/mui/css/icons-extra.css'
// import { Header } from 'mint-ui'
// Vue.component(Header.name, Header)
// import { Swipe, SwipeItem,Button} from 'mint-ui';
// import { Lazyload } from 'mint-ui';
import MintUI from 'mint-ui'
Vue.use(MintUI)
import 'mint-ui/lib/style.css'
import VuePreview from 'vue-preview'
Vue.use(VuePreview)
// Vue.use(Lazyload);
// Vue.component(Button.name, Button)
import { from } from '_array-flatten@2.1.2@array-flatten'
// Vue.component(Swipe.name, Swipe);
// Vue.component(SwipeItem.name, SwipeItem);

var vm = new Vue({
  el: '#app',
  render: c => c(app),
  router,
  store
})
